package fair.controller;

import fair.entity.Result;
import fair.pojo.Classes;
import fair.pojo.User;
import fair.pojo.UserClass;
import fair.service.IClassService;
import fair.service.IUserClassService;
import fair.service.IUserService;
import fair.utils.Md5Util;
import fair.utils.PoiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author fair_wu
 * @version v1.0
 * @date 2021-03-06 下午 12:49
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping("admin")
public class AdminController {

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IUserService userService;

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IClassService classService;

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IUserClassService iUserClassService;

    /**
     * 使用excel批量上传学生账号信息
     * @param file
     * @return
     */
    @ApiOperation("使用excel批量上传学生账号信息")
    @RequestMapping(value = "addStuExcel", method = RequestMethod.POST)
    public Result addStudentExcel(@RequestParam(value = "file", required = true) MultipartFile file){
        try {
            List<String[]> lists = PoiUtils.readExcel(file);
            String classname = lists.get(0)[3];
            Map<String, Object> map = new HashMap<>();
            map.put("classname", classname);
            Collection<Classes> classList = classService.listByMap(map);
            Iterator<Classes> iterator = classList.iterator();
            Classes classes = iterator.next();
            Integer classId = classes.getId();
            User user = new User();
            UserClass userClass = new UserClass();
            for (String[] list : lists) {
                user.setUsername(list[0]);
                user.setPassword(Md5Util.encode("root"));
                user.setName(list[1]);
                user.setCollege(list[2]);
                user.setRole("student");
                user.setClassname(classname);
                user.setPhone(list[4]);
                user.setEmail(list[5]);
                Integer userID = userService.insert(user);
                userClass.setClassId(classId);
                userClass.setUserId(userID);
                iUserClassService.save(userClass);
            }
            return new Result(0, "", lists.size());
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 添加老师账号
     * @param user
     * @param classIds
     * @return
     */
    @ApiOperation("添加老师账号")
    @RequestMapping(value = "addTea", method = RequestMethod.POST)
    @ApiImplicitParam(name = "classIds", value = "classIds", paramType = "query")
    public Result addTeacher(@RequestBody User user, @RequestParam(value = "classIds") Integer[] classIds) {
        user.setPassword(Md5Util.encode("root"));
        user.setRole("teacher");
        Integer id = userService.insert(user);
        UserClass userClass = new UserClass();
        userClass.setUserId(id);
        try {
            for (Integer classId : classIds) {
                userClass.setClassId(classId);
                iUserClassService.save(userClass);
            }
            return new Result(0, "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 添加学生账号
     * @param user
     * @param classId
     * @return
     */
    @ApiOperation("添加学生账号")
    @RequestMapping(value = "addStu", method = RequestMethod.POST)
    public Result addTeacher(@RequestBody User user, @RequestParam(value = "classId") Integer classId) {
        try {
            user.setPassword(Md5Util.encode("root"));
            Classes classes = classService.getById(classId);
            user.setRole("student");
            user.setClassname(classes.getClassname());
            Integer id = userService.insert(user);
            UserClass userClass = new UserClass();
            userClass.setUserId(id);
            iUserClassService.save(userClass);
            return new Result(0, "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 通过用户名重置密码
     * @param username
     * @return
     */
    @ApiOperation("重置密码")
    @RequestMapping(value = "reset", method = RequestMethod.POST)
    public Result resetPassword(@RequestParam String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        Collection<User> users = userService.listByMap(map);
        if (users.size() == 0)
            return new Result(403, "不存在该用户");
        User user = users.iterator().next();
        user.setPassword(Md5Util.encode("123456"));
        try {
            userService.updateById(user);
            return new Result(0, "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 查询所有的老师
     * @return
     */
    @ApiOperation("查询所有的老师")
    @RequestMapping(value = "findAllTea", method = RequestMethod.GET)
    public Result findAllTea(@RequestParam(value = "pageNum", defaultValue = "1") String pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") String pageSize){

        try {
            List<User> users = userService.findTeaByPage(pageNum, pageSize);
            return new Result(0, "", users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 查询所有的学生
     * @return
     */
    @ApiOperation("查询所有的学生")
    @RequestMapping(value = "findAllStu", method = RequestMethod.GET)
    public Result findAllStu(@RequestParam(value = "pageNum", defaultValue = "1") String pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") String pageSize){
        try {
            List<User> users = userService.findStuByPage(pageNum, pageSize);
            return new Result(0, "", users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    /**
     * 按用户名查找用户
     * @return
     */
    @ApiOperation("按用户名查找用户")
    @RequestMapping(value = "findByUsername", method = RequestMethod.GET)
    public Result findByUsername(@RequestParam String username){
        try {
            User user = userService.findOne(username);
            return new Result(0, "", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }
}
